//
//  StartBottomCell.swift
//  socksTest
//
//  Created by liujianlong on 2023/6/26.
//

import UIKit

class StartBottomCell: UITableViewCell {
    @IBOutlet var controlBtn: UIButton!
    @IBOutlet var switchBtn: UISwitch!
    @IBOutlet var endBtn: UIButton!
    let popView : MMPopViewController = MMPopViewController()
    var block:((_ title:String)->Void)?
    var switchBlock:((_ on:Bool)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        popView.array = ["单独控制","合并控制"]
        popView.delegate = self
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    @IBAction func actionForBtn(_ sender: UIButton) {
        if sender.tag == 201{
            self.popView.modalPresentationStyle = .popover
            self.popView.preferredContentSize = CGSize(width:150, height:self.popView.array.count * 50)
            self.popView.popover = self.popView.popoverPresentationController
            self.popView.popover?.delegate = self
            self.popView.popover!.permittedArrowDirections = .up
            self.popView.popoverPresentationController?.sourceView = sender
            self.popView.popoverPresentationController?.sourceRect = sender.bounds
            self.currentVC().present(self.popView, animated:true, completion:nil)
        }else{
            block?(sender.currentTitle ?? "")
        }
    }
    
    @IBAction func actionForSwitch(_ sender: UISwitch) {
        switchBlock?(sender.isOn)
    }
}


//MARK: - MMPopViewControllerDelegate (UIPopoverPresentationControllerDelegate)
extension StartBottomCell : MMPopViewControllerDelegate{
    func select(_ index: Int, _ title: String) {
        self.controlBtn.setTitle(title, for: .normal)
        block?(title)
    }
    
    /// 默认返回的是覆盖整个屏幕，需设置成UIModalPresentationNone。
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
    
    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }
}

